| Total Complexity | 9 |
| Total Lines | 63 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import {Event, EventType} from './Event.entity'; |
||
| 3 | |||
| 4 | export class GetEventsOverview { |
||
| 5 | public index(events: Event[]): IEventsOverview { |
||
| 6 | const eventsByDate = []; |
||
| 7 | const overview: IEventsOverview = { |
||
| 8 | mission: 0, |
||
| 9 | dojo: 0, |
||
| 10 | formationConference: 0, |
||
| 11 | holiday: 0, |
||
| 12 | medicalLeave: 0, |
||
| 13 | support: 0, |
||
| 14 | workFree: 0, |
||
| 15 | other: 0, |
||
| 16 | mealTicket: 0 |
||
| 17 | }; |
||
| 18 | |||
| 19 | for (const event of events) { |
||
| 20 | const dayIndex = new Date(event.getDate()).getDate() - 1; |
||
| 21 | const time = event.getTime() / 100; |
||
| 22 | const type = event.getType(); |
||
| 23 | |||
| 24 | if (eventsByDate[dayIndex]) { |
||
| 25 | eventsByDate[dayIndex].push({time, type}); |
||
| 26 | } else { |
||
| 27 | eventsByDate[dayIndex] = [{time, type}]; |
||
| 28 | } |
||
| 29 | |||
| 30 | overview[event.getType()] += time; |
||
| 31 | } |
||
| 32 | |||
| 33 | return this.calculateNumberOfMealTicket(overview, eventsByDate); |
||
| 34 | } |
||
| 35 | |||
| 36 | public calculateNumberOfMealTicket( |
||
| 37 | overview: IEventsOverview, |
||
| 38 | eventsByDate: any[] |
||
| 39 | ): IEventsOverview { |
||
| 40 | const excludedType = [ |
||
| 41 | EventType.WORK_FREE, |
||
| 42 | EventType.MEDICAL_LEAVE, |
||
| 43 | EventType.HOLIDAY, |
||
| 44 | EventType.OTHER |
||
| 45 | ]; |
||
| 46 | |||
| 47 | for (const sortedEvent of eventsByDate) { |
||
| 48 | if (!sortedEvent) { |
||
| 49 | continue; |
||
| 50 | } |
||
| 51 | |||
| 52 | let totalPerDay = 0; |
||
| 53 | |||
| 54 | for (const {time, type} of sortedEvent) { |
||
| 55 | if (!excludedType.includes(type)) { |
||
| 56 | totalPerDay += time; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | if (totalPerDay > 0.5) { |
||
| 61 | overview.mealTicket++; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | return overview; |
||
| 66 | } |
||
| 68 |